// /*
//  * @Author: WanYiMing
//  * @Date:2024-07-05-10:17
//  * @Description:
//  */

using System;
using System.Collections.Generic;
using UnityEngine;

namespace Code.GameLogic.XianJian.Element
{
    public class JianQiPoll
    {
        
        private readonly Queue<GameObject> _poolQueue = new Queue<GameObject>();

        private GameObject _XianJian;
        private GameObject _jianQiPrefab;
        private CreateJianQi _createObjectFunction;
        
        public JianQiPoll(GameObject jianQiPrefab,GameObject XianJian,int initialSize,CreateJianQi createObjectFunction)
        {
            _jianQiPrefab = jianQiPrefab;
            _XianJian = XianJian;
            _createObjectFunction = createObjectFunction;
            for (int i = 0; i < initialSize; i++)
            {
                var obj = BuildingJianQi();
                obj.SetActive(false);
                _poolQueue.Enqueue(obj);
            }
        }

        private GameObject BuildingJianQi()
        {
            GameObject obj = _createObjectFunction(_jianQiPrefab, _XianJian.transform.position, Quaternion.identity);
            obj.transform.parent = _XianJian.transform;
            return obj;
        }


        public GameObject GetJianQi()
        {
            var buildingJianQi = _poolQueue.Count > 0 ? _poolQueue.Dequeue() : BuildingJianQi();
            buildingJianQi.SetActive(true);
            return buildingJianQi;
        }
        
        public void ReleaseJianQi(GameObject obj)
        {
            obj.SetActive(false);
            _poolQueue.Enqueue(obj);
        }
        
    }

    public delegate GameObject CreateJianQi(GameObject original, Vector3 position, Quaternion rotation);
}